package model

import (
	"encoding/json"
	"errors"
	"time"
)

// 定义结构体信息
type SignUpForm struct {
	Email           string `json:"email"`
	Password        string `json:"password"`
	ConfirmPassword string `json:"confirm_password"`
}

// UnmarshalJSON SignUpForm 类型 自定义 JSON 反序列化的方法
func (s *SignUpForm) UnmarshalJSON(data []byte) (err error) {
	required := struct {
		Email           string `json:"email"`
		Password        string `json:"password"`
		ConfirmPassword string `json:"confirm_password"`
	}{}
	err = json.Unmarshal(data, &required)
	if err != nil {
		return
	} else if len(required.Email) == 0 {
		err = errors.New("缺少必填字段 邮箱")
	} else if len(required.Password) == 0 {
		err = errors.New("缺少必填字段 密码")
	} else if required.Password != required.ConfirmPassword {
		err = errors.New("两次密码不一致")
	} else {
		s.Email = required.Email
		s.Password = required.Password
		s.ConfirmPassword = required.ConfirmPassword
	}
	return
}

type User struct {
	UID         int       `json:"uid" db:"uid"`
	UserSn      uint64    `json:"userSn" db:"user_sn"`
	Nickname    string    `json:"nickname" db:"nickname"`
	Email       string    `json:"email" db:"email"`
	Password    string    `json:"password"`
	Avatar      string    `json:"avatar" db:"avatar"`
	Gender      uint8     `json:"gender" db:"gender"`
	Introduce   string    `json:"introduce" db:"introduce"`
	State       uint8     `json:"state" db:"state"`
	IsRoot      bool      `json:"isRoot" db:"is_root"`
	StorePasswd []byte    `json:"storePasswd" db:"store_passwd"`
	CreatedAt   time.Time `json:"createdAt" db:"created_at"`
	UpdatedAt   time.Time `json:"updatedAt" db:"updated_at"`
}

type UserCount struct {
	UID        uint64 `json:"uid" db:"uid"`
	FansNum    int    `json:"fansNum" db:"fans_num"`
	FollowNum  int    `json:"followNum" db:"follow_num"`
	ArticleNum int    `json:"articleNum" db:"article_num"`
	Words      int    `json:"words" db:"words"`
	ZanNum     int    `json:"zanNum" db:"zan_num"`
}
